package com.chujian.system.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chujian.client.wx.WxMaClient;
import com.chujian.framework.core.base.BaseServiceImpl;
import com.chujian.framework.core.tsdata.Result;
import com.chujian.framework.exception.MsgException;
import com.chujian.sys.entity.SysUser;
import com.chujian.sys.entity.SysUserLogin;
import com.chujian.system.sys.mapper.SysUserLoginMapper;
import com.chujian.system.sys.service.SysUserLoginService;
import com.chujian.system.sys.service.SysUserService;
import com.chujian.wx.entity.WxMinipUser;
import com.chujian.wx.vo.GetPhoneInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
 * 服务实现类
 *
 * @author chujian
 * @since 2021-10-11
 */
@Service
@Slf4j
public class SysUserLoginServiceImpl extends BaseServiceImpl<SysUserLoginMapper, SysUserLogin> implements SysUserLoginService {
    @Autowired
    private SysUserService userService;

    public static final String LOGIN_TYPE_MA = "ma";
    public static final String LOGIN_TYPE_MP = "mp";

    @Autowired
    private WxMaClient wxMaClient;

    @Override
    public SysUserLogin saveSysUserLogin(WxMinipUser maUser) {
        SysUserLogin exi = this.getByOpenId(maUser.getOpenId());
        SysUserLogin userLogin = new SysUserLogin()
                .setWxMinipOpenId(maUser.getOpenId())
                .setWxUnionId(maUser.getUnionId())
                .setLoginType(LOGIN_TYPE_MA)
                .setWxNickname(maUser.getNickname())
                .setWxHeadUrl(maUser.getHeadImgUrl())
                .setWxGender(maUser.getGender())
                .setSex("1".equals(maUser.getGender()) ? 1 : 2);
        if (null == exi) { // 新增
            userLogin.setUserId(0L);
            this.save(userLogin);
        } else {
            userLogin.setId(exi.getId());
            this.updateById(userLogin);
        }
        return this.getById(userLogin.getId());
    }

    @Override
    public SysUserLogin getByOpenId(String openId) {
        return this.getOne(new LambdaQueryWrapper<SysUserLogin>().eq(SysUserLogin::getWxMinipOpenId, openId));
    }

    @Override
    public SysUserLogin parseMaUserPhone(String appId, GetPhoneInfoVo getPhoneInfoVo) {
        Result<String> result = wxMaClient.getUserPhone(appId, getPhoneInfoVo);
        if (!result.isSuccess()) {
            throw new MsgException("手机号解析失败");
        }
        SysUserLogin userLogin = getByOpenId(getPhoneInfoVo.getOpenId());
        this.updateById(
                new SysUserLogin()
                        .setUserLoginId(userLogin.getId())
                        .setMobile(result.getData())
        );
        return this.getById(userLogin.getId());
    }

    @Override
    public void bindSysWxUser(Long userId, Long userLoginId) {
        SysUser user = userService.getById(userId);
        SysUserLogin userLogin = this.getById(userLoginId);
        if (null != user) {
            userLogin.setUserId(userId);
            userLogin.setBindTime(LocalDateTime.now());
            this.upd(userLogin);

            // 清除用户的微信相关信息
            userService.clearUserWxInfos(userId);
        }
    }

    @Override
    public SysUserLogin saveRealName(String appId, SysUserLogin userLogin) {
        this.updateById(
                new SysUserLogin()
                        .setUserLoginId(userLogin.getUserLoginId())
                        .setUserName(userLogin.getUserName())
        );
        return this.getById(userLogin.getId());
    }

    @Override
    public SysUserLogin getMaUserLoginByUserId(Long userId) {
        return this.getOne(
                new LambdaQueryWrapper<SysUserLogin>()
                        .eq(SysUserLogin::getUserId, userId)
                        .eq(SysUserLogin::getLoginType, LOGIN_TYPE_MA)
        );
    }
}
